package com.lcq.gulimall.product.service.impl;

import com.lcq.gulimall.product.vo.Images;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.product.dao.SkuImagesDao;
import com.lcq.gulimall.product.entity.SkuImagesEntity;
import com.lcq.gulimall.product.service.SkuImagesService;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuImagesEntity> page = this.page(
                new Query<SkuImagesEntity>().getPage(params),
                new QueryWrapper<SkuImagesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveImages(Long skuId, List<Images> skuImages) {
        if(skuImages==null||skuImages.size()==0){
            return;
        }
        List<SkuImagesEntity> imagesEntities = skuImages.stream().map((img)->{
            SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
            skuImagesEntity.setImgUrl(img.getImgUrl());
            skuImagesEntity.setDefaultImg(img.getDefaultImg());
            skuImagesEntity.setSkuId(skuId);
            return skuImagesEntity;
        }).filter((entity)->{
            return !StringUtils.isNullOrEmpty(entity.getImgUrl());
        }).collect(Collectors.toList());
        this.saveBatch(imagesEntities);
    }
}